﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using MyExcelAddIn2003.ComOPHelper;
using MyExcelAddIn2003.Core;

namespace MyExcelAddIn2003.Weight
{
    public partial class listFrm : Form
    {
        public listFrm()
        {
            InitializeComponent();
            try
            {
                VBAfunc.VBA_listinit();
                Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddIn.Application.Worksheets["查询"];
                dataGridView1.DataSource = rangeTable(ws.get_Range("查询结果", Type.Missing));
                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                {
                    comboBox_SearchParam.Items.Add(dataGridView1.Columns[i].HeaderText);
                }
                comboBox_SearchParam.Text = "车牌号码";
                textBox_SearchValue.Focus();
            }
            catch
            {
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string WTID = dataGridView1.Rows[e.RowIndex].Cells["磅单编码"].Value.ToString();
                Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddIn.Application.Worksheets[Utility.BillWorkSheet];
                ws.get_Range("磅单编码", Type.Missing).Value2 = WTID;
                VBAfunc.listFrm_dataGridView1_CellDoubleClick();
                //VBA脚本并不会触发Worksheet_Change事件，故而在下面加入以下代码以满足界面数据的现实问题。
                OPHelper op = new OPHelper();
                op.SetSheetToMainForm();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            this.Close();
        }

        private void textBox_SearchValue_TextChanged(object sender, EventArgs e)
        {
            string strFilter ="";
            DataTable dt;
            if (comboBox_SearchParam.Text != "")
            {
                if (textBox_SearchValue.Text != "")
                {
                    strFilter = String.Format("{0} like '%{1}%'", comboBox_SearchParam.Text, textBox_SearchValue.Text.Trim());
                }
                else
                {
                    strFilter = "";
                }
            }
            dt = (DataTable)dataGridView1.DataSource;
            dt.DefaultView.RowFilter = strFilter;
        }

        int rowindex = -1;
        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                dataGridView1.ClearSelection();
                dataGridView1.Rows[e.RowIndex].Selected = true;
                rowindex = e.RowIndex;
                ContextMenuStrip contextMenuStrip1 = new ContextMenuStrip();
                contextMenuStrip1.Items.Add("删除");
                contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                contextMenuStrip1.Click += new EventHandler(contextMenuStrip1_ItemClicked);
            }
        }

        private void contextMenuStrip1_ItemClicked(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentRow != null)
            {
                if (rowindex != -1)
                {
                    //MessageBox.Show("删除" + dataGridView1.Rows[rowindex].Cells["磅单编码"].Value.ToString());
                    VBAfunc.DelFirstWTData(dataGridView1.Rows[rowindex].Cells["磅单编码"].Value.ToString());
                }
            }
        }
        public DataTable rangeTable(Microsoft.Office.Interop.Excel.Range rng)
        {
            DataTable dt = new DataTable();
            int rowCnt = rng.Rows.Count;
            int colCnt = rng.Columns.Count;

            for (int i = 1; i <= rowCnt; i++)
            {
                DataRow newRow = dt.NewRow();
                for (int j = 1; j <= colCnt; j++)
                {
                    if (i == 1)
                    {
                        dt.Columns.Add(((Excel.Range)rng.get_Item(i, j)).Value2.ToString(), typeof(string));
                    }
                    else
                    {
                        newRow[j - 1] = ((Excel.Range)rng.get_Item(i, j)).Value2;
                    }
                }
                if (i > 1)
                {
                    dt.Rows.Add(newRow);
                }
            }
            return dt;

        }
    }

}
